<template>
  <div class="cotn_principal" :style="back">

    <div :style="back" style="width: 100vw;height: 85vh; position: fixed;left: 0; ">

    </div>


    <div style="margin: 50% 40% 0 0 ">

      <el-card style="width: 90vw; height: 35vh; margin-left: 10%;opacity: 0.9; border-radius: 22px">
        <div style="left: 0;margin-left: -25%;">
          <el-form label-width="80px" style="" :model="user" :rules="rules" ref="LoginForm">


            <el-form-item prop="name">
              <el-input v-model="user.name" style="width: 80vw" prefix-icon="el-icon-s-custom" placeholder="请输入昵称"></el-input>
            </el-form-item>

            <el-form-item prop="username">
              <el-input v-model="user.username" style="width: 80vw" prefix-icon="el-icon-user" placeholder="请输入账号"></el-input>
            </el-form-item>

            <el-form-item prop="password">
              <el-input v-model="user.password" type="password" style="width: 80vw" prefix-icon="el-icon-lock" placeholder="请输入密码"></el-input>
            </el-form-item>

            <el-form-item prop="confirmPassword">
              <el-input v-model="user.confirmPassword" type="password" style="width: 80vw" prefix-icon="el-icon-lock"
                        placeholder="请再次输入密码"></el-input>
            </el-form-item>
            <div style="display: flex; justify-content: center ;width: 80vw; margin-left: 18%">
              <el-button type="success" style="width: 20vw; height: 4vh" @click="$router.push('/login')">返回登录
              </el-button>
              <el-button type="danger" style="width: 20vw; height: 4vh" @click="register">注册</el-button>
            </div>
          </el-form>
        </div>
      </el-card>


    </div>


  </div>
</template>

<script>
import Cookies from "js-cookie";
import request from "@/utils/request";

export default {
  name: "Register",
  data() {
    return {
      loginUser: {},
      user: {},
      rules: {
        name: [
          {required: true, message: '请输入您的昵称', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在3到10之间', trigger: 'blur'}
        ],
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在3到10之间', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 8, max: 16, message: '长度在8到16之间', trigger: 'blur'}
        ],
        confirmPassword: [
      {required: true, message: '请再次输入密码', trigger: 'blur'},
      {min: 8, max: 16, message: '长度在8到16之间', trigger: 'blur'}
    ]
      },

      back: {
        backgroundImage: 'url(' + require('../assets/b.png') + ')',
        backgroundRepeat: 'no-repeat',
      },


    }
  },

  methods: {
    register() {
      this.$refs['LoginForm'].validate((valid) => {
        if (valid) {
          if (this.user.password !== this.user.confirmPassword) {
            this.$message.error('两次输入的密码不一致');
            return false;
          }
          request.post('/user/register', this.user).then(res => {
            if (res.code === '200') {
              this.loginUser = res.data
              this.$notify.success("账号创建成功！")
              this.$router.push('/login')
            } else {
              this.$notify.error(res.msg)
            }
          })
        }
      })

    },

  }
}
</script>

<style scoped>

.cover {
  width: fit-content;
  background-color: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1000;
}


* {
  text-align: center;
  margin: 0 auto;
  padding: 0;
  font-family: "Open Sans", sans-serif;
}

.cotn_principal {
  position: absolute;
  width: 100%;
  height: 100%;
  background: #cfd8dc;
  background: -moz-linear-gradient(
      -45deg,
      #cfd8dc 0%,
      #607d8b 100%,
      #b0bec5 100%
  );
  background: -webkit-linear-gradient(
      -45deg,
      #cfd8dc 0%,
      #607d8b 100%,
      #b0bec5 100%
  );
  background: linear-gradient(
      135deg,
      #cfd8dc 0%,
      #607d8b 100%,
      #b0bec5 100%
  );
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cfd8dc', endColorstr='#b0bec5', GradientType=1);
}
</style>